
/*
typedef pipe<k3conn_job>::kernel_1a k3conn_jop_pipe;

class k3connection : public threaded_object //Replace to OnRequest
{
    k3connection();
    ~k3connection();
    void    thread();
    k3conn_jop_pipe job_pipe;
};


k3connection::k3connection()
  : job_pipe (10)
{
    _k3cache_manager.create_cache(NONTRANSACTED, this);
}


k3connection::~k3connection()
{
    stop();
    wait();
    k3conn_job j(K3CLEANCONN_CACHE);
    _k3cache_manager.m_jobs.queue(j, this);
}

void
k3connection::thread()
{
    // This is our thread.  It will loop until it is told that it should terminate.
    k3conn_job j;
    // Here we loop on jobs from the job_pipe.
    while (!should_stop() && job_pipe.dequeue(j))
    {
        // process our job j in some way.
        k3dlog << LINFO << "k3connection::thread(). got job " << j.id;

        switch (j.id)
        {
            case k3conn_job::begin_trans;
                t_id = _k3cache_manager.create_trans(this);
                send(t_id);
                break;
            case K3INSERT:
                _k3cache_manager.m_jobs.queue(j, this);
                m_status = WAIT_CACHE;
                break;
            case K3CREATE_CURSOR:
                foreach()
                {
                    _k3cache_manager.m_jobs.queue(j, this);
                    m_status = WAIT_CACHE;
                }
                save_cursor();

                break;

        };

        // sleep for 0.1 seconds
        dlib::sleep(100);
    }
    dlog << LINFO << "k3connection::thread(). thread ending";
}

//================================ realisation ==================================

class k3conn_manager : public thread_pool  REPLACE to server
/// single instance. Catches connect jobs from client, then creates connection and returns its ID
/// 2. every connection thread works autonomous with cache_manager and network
/// 3. administrator thread. stops, restarts threads including manager
{
public:
    k3conn_manager();
    ~k3conn_manager();
    uint64  conn_create();  // return new id
    void    conn_drop(uint64 id);
    map<uint64, k3connection*> connects;
private:
    void th_conn();
    void th_admin();
    void th_manager();
}


k3conn_manager::k3conn_manager()
    : thread_pool(5),  // 2 control + 3 connection
      job_pipe(10)  // queue length
{
    add_task(*this, th_admin);
    add_task(*this, th_manager);
    add_task(*this, th_conn);
}


k3conn_manager::~k3conn_manager()
{
    wait_for_all_tasks();
    k3dlog << LINFO << "k3conn_manager::~k3conn_manager(). all tasks finished";
}


k3conn_manager::th_manager()
{
    k3conn_job j;
    // Here we loop on jobs from the job_pipe.
    while (job_pipe.dequeue(j))
    {
        // process our job j in some way.
        k3dlog << LINFO << "k3conn_manager::th_manager(). got job " << j.id;

        // sleep for 0.1 seconds
        dlib::sleep(100);
    }
    dlog << LINFO << "k3conn_manager::th_manager(). thread ending";

}


uint64
k3conn_manager::conn_create()
{
    dlog << LINFO << "k3conn_manager::conn_create(). requested";

    k3connection* conn = new k3connection();
    uint64 id = add_task(*this, &conn->thread);
    connects.insert(make_pair<uint64, k3connection*>(id, conn));
    conn->start();

    dlog << LINFO << "k3conn_manager::conn_create(). created #" << id;

    return id;
}


void
k3conn_manager::conn_drop(uint64 id)
{
    dlog << LINFO << "k3conn_manager::conn_drop(). request #" << id;

    if (connects.contains(id))
    {
        delete connects[id].second;  // also destructor handles stop()
        connects.erase(id);
        dlog << LINFO << "k3conn_manager::conn_drop(). finished #" << id;
    }

}
*/
/*
#include "dlib/server.h"
#include "dlib/sockstreambuf.h"
#include "dlib/ref.h"
#include <iostream>

using namespace dlib;
using namespace std;



class serv : public server::kernel_1a_c
{

    void on_connect  (
        connection& con
    )
    {
        // create a sockstreambuf that reads/writes on our connection.  I'm using the
        // kernel_2a version here because it is generally the faster of the two versions in the
        // library.
        sockstreambuf::kernel_2a buf(&con);

        // Now we make an iostream object that reads/writes to our streambuffer.  A lot of people
        // don't seem to know that the C++ iostreams are as powerful as they are.  So what I'm doing
        // here isn't anything special and is totally portable.  You will be able to use this stream
        // object just as you would any iostream from the standard library.
        iostream stream(&buf);

        // This command causes our stream to flush its output buffers whenever you ask it for more
        // data.
        stream.tie(&stream);

        char ch;
        while (stream.good())
        {
            // get the next character from the client
            ch = stream.get();

            // now echo it back to them
            stream << (char)toupper(ch);
        }
    }

};


void thread(serv& our_server)
{
    try
    {
        // Start the server.  start() blocks until the server is shutdown
        // by a call to clear()
        our_server.start();
    }
    catch (socket_error& e)
    {
        cout << "Socket error while starting server: " << e.what() << endl;
    }
    catch (exception& e)
    {
        cout << "Error while starting server: " << e.what() << endl;
    }
}


int main()
{
    try
    {
        serv our_server;

        // set up the server object we have made
        our_server.set_listening_port(1234);
        our_server.set_max_connections(1000);

        // create a thread that will start the server.   The ref() here allows us to pass
        // our_server into the threaded function by reference.
        thread_function t(thread, ref(our_server));

        cout << "Press enter to end this program" << endl;
        cin.get();
        // this will cause the server to shut down
        our_server.clear();
    }
    catch (exception& e)
    {
        cout << e.what() << endl;
    }
    catch (...)
    {
        cout << "Some error occurred" << endl;
    }
}
*/

